package com.qingcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.TbMenuMapper;
import com.qingcheng.pojo.system.TbMenu;
import com.qingcheng.service.TbMenuService;
import com.qingcheng.util.PageUtil;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
* @author Simon
* @date 2020/08/06
*/
@Service
public class TbMenuServiceImpl implements TbMenuService {

    @Resource
    private TbMenuMapper tbMenuMapper;


    @Override
    public List<Map> queryAll() {
        List<TbMenu> tbMenus = tbMenuMapper.selectAll();
        List<Map> menuListByParentId = findMenuListByParentId(tbMenus, "0");
        return menuListByParentId;
    }

    /**
     * 查询下级菜单，根据传过来的列表和父级id去找下级菜单
     * @param menuList 菜单列表
     * @param parentId 父id
     * @return
     */
    private List<Map> findMenuListByParentId(List<TbMenu> menuList,String
            parentId){
        List<Map> mapList=new ArrayList<>();
        for(TbMenu menu:menuList){ //循环一级菜单
            if(menu.getParentId().equals(parentId)){
                Map map=new HashMap();
                map.put("path",menu.getId());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                map.put("linkUrl",menu.getUrl());
                map.put("children",findMenuListByParentId(menuList,menu.getId()));
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public PageUtil<TbMenu> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbMenu> pages = (Page<TbMenu>) tbMenuMapper.selectAll();
        PageUtil<TbMenu> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbMenu> pageQueryByAll(Integer pageNum, Integer pageSize, TbMenu tbMenu) {

        Example example=new Example(TbMenu.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name",brand.getName());
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbMenu> pages = (Page<TbMenu>) tbMenuMapper.selectByExample(example);
        PageUtil<TbMenu> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbMenu(TbMenu tbMenu) {
        int insert = tbMenuMapper.insert(tbMenu);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbMenu(TbMenu tbMenu) {
        int i = tbMenuMapper.updateByPrimaryKeySelective(tbMenu);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbMenuMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbMenu findById(Integer id) {
        TbMenu tbMenu = tbMenuMapper.selectByPrimaryKey(id);
        return tbMenu;
    }
}
